package com.dietmanager.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * The Class DailyLogModel is a model for daily logs.
 */
public class DailyLogModel implements Serializable {
	public static final int DEFAULT_CALORIE_LIMIT = 1000;
	public static final int DEFAULT_WEIGHT = 73;
	private static final long serialVersionUID = 1615355243903591525L;
	private Map<String, DailyLog> logArr = new HashMap<String, DailyLog>();
	
	/**
	 * Checks for daily log with specific date.
	 *
	 * @param date the date
	 * @return true, if successful
	 */
	public boolean hasDailyLog(Date date) {
		return (logArr.get(convertDateToString(date)) == null) ? false : true;
	}
	
	/**
	 * Adds the daily log.
	 *
	 * @param date the date
	 */
	public void addDailyLog(Date date) {
		logArr.put(convertDateToString(date), new DailyLog());
	}
	
	/**
	 * Gets the foods.
	 *
	 * @param date the date
	 * @return the foods
	 */
	public ArrayList<FoodModel> getFoods(Date date) {
		if(logArr.get(convertDateToString(date)) != null)
		{
			return logArr.get(convertDateToString(date)).getFoodArr();
		}
		return null;
	}
	
	/**
	 * Gets the foods.
	 *
	 * @param date the date
	 * @return the foods
	 */
	public ArrayList<ExerciseModel> getExercises(Date date) {
		if(logArr.get(convertDateToString(date)) != null)
		{
			return logArr.get(convertDateToString(date)).getExerciseArr();
		}
		return null;
	}

	/**
	 * Gets the weight.
	 *
	 * @param date the date
	 * @return the weight
	 */
	public int getWeight(Date date) {
		return logArr.get(convertDateToString(date)).getWeight();
	}

	/**
	 * Gets the calorie limit.
	 *
	 * @param date the date
	 * @return the calorie limit
	 */
	public int getCalorieLimit(Date date) {
		return logArr.get(convertDateToString(date)).getCalorieLimit();
	}

	/**
	 * Adds the food.
	 *
	 * @param date the date
	 * @param food the food
	 */
	public void addFood(Date date, FoodModel food) {
		logArr.get(convertDateToString(date)).addFood(food);
	}

	/**
	 * Removes the food.
	 *
	 * @param date the date
	 * @param food the food
	 */
	public void removeFood(Date date, FoodModel food) {
		logArr.get(convertDateToString(date)).removeFood(food);
	}

	/**
	 * Adds the exercise.
	 *
	 * @param date the date
	 * @param exercise the exercise
	 */
	public void addExercise(Date date, ExerciseModel exercise) {
		logArr.get(convertDateToString(date)).addExercise(exercise);
	}
	
	/**
	 * Removes the exercise.
	 *
	 * @param date the date
	 * @param exercise the exercise
	 */
	public void removeExercise(Date date, ExerciseModel exercise) {
		logArr.get(convertDateToString(date)).removeExercise(exercise);
	}

	/**
	 * Sets the weight.
	 *
	 * @param date the date
	 * @param weight the weight
	 */
	public void setWeight(Date date, int weight) {
		logArr.get(convertDateToString(date)).setWeight(weight);
	}

	/**
	 * Sets the calorie limit.
	 *
	 * @param date the date
	 * @param calorieLimit the calorie limit
	 */
	public void setCalorieLimit(Date date, int calorieLimit) {
		logArr.get(convertDateToString(date)).setCalorieLimit(calorieLimit);
	}

	/**
	 * Convert date to string.
	 *
	 * @param date the date
	 * @return the string
	 */
	private String convertDateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 * Gets the previous date.
	 *
	 * @param date the date
	 * @return the previous date
	 */
	public Date getPreviousDate(Date date) {
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);
	    cal.add(Calendar.DAY_OF_MONTH, -1);
	    return cal.getTime();
	}
	
	/**
	 * The Class DailyLog.
	 */
	private class DailyLog implements Serializable  {
		private static final long serialVersionUID = -5002130147233763934L;
		private ArrayList<FoodModel> foodArr = new ArrayList<FoodModel>();
		private ArrayList<ExerciseModel> exerciseArr = new ArrayList<ExerciseModel>();
		private int weight;
		private int calorieLimit;
		
		/**
		 * Gets the calorie limit.
		 *
		 * @return the calorie limit
		 */
		public int getCalorieLimit() {
			return calorieLimit;
		}
		
		/**
		 * Sets the calorie limit.
		 *
		 * @param calorieLimit the new calorie limit
		 */
		public void setCalorieLimit(int calorieLimit) {
			this.calorieLimit = calorieLimit;
		}
		
		/**
		 * Gets the weight.
		 *
		 * @return the weight
		 */
		public int getWeight() {
			return weight;
		}
		
		/**
		 * Sets the weight.
		 *
		 * @param weight the new weight
		 */
		public void setWeight(int weight) {
			this.weight = weight;
		}
		
		/**
		 * Gets the food array.
		 *
		 * @return the food array
		 */
		public ArrayList<FoodModel> getFoodArr() {
			return foodArr;
		}
		
		/**
		 * Gets the exercise array.
		 *
		 * @return the exercise array
		 */
		public ArrayList<ExerciseModel> getExerciseArr() {
			return exerciseArr;
		}
		
		/**
		 * Adds a food.
		 *
		 * @param food the food
		 */
		public void addFood(FoodModel food) {
			this.foodArr.add(food);
		}

		/**
		 * Removes a food.
		 *
		 * @param food the food
		 */
		public void removeFood(FoodModel food) {
			this.foodArr.remove(food);
		}

		/**
		 * Adds a exercise.
		 *
		 * @param exercise the exercise
		 */
		public void addExercise(ExerciseModel exercise) {
			this.exerciseArr.add(exercise);
		}

		/**
		 * Removes a exercise.
		 *
		 * @param exercise the exercise
		 */
		public void removeExercise(ExerciseModel exercise) {
			this.exerciseArr.remove(exercise);
		}
	}
}
